<?php

class menu extends baseController {
	protected  $type;
	public function __construct() {
		parent::__construct();
		$this->type = SessionUtil::getVar(SESSION_CURRENT_TOP_MENU_KEY);
		$this->controller = $this->type;
	}
	public function index() {
		$menu_db_obj = $this->getMenuDbObj();
		
		if ( null == SessionUtil::getVar('ui_view_show_setting') ) {
			SessionUtil::setVar('ui_view_show_setting','Y');
		}
		$conditions = array();
		$this->show_setting = SessionUtil::getVar('ui_view_show_setting');
		if ( 'all' == $this->show_setting ) {
		
		} else if ( 'Y' == $this->show_setting ) {
			$conditions['active'] = 'Y';
		} else if ( 'N' == $this->show_setting ) {
			$conditions['active'] = 'N';
		}
		$conditions = count($conditions) > 0 ? $conditions : null;
		$this->menu_list = $menu_db_obj->findAll($conditions,'`order` ASC');
	}
	function edit(){
		$uuid = $this->spArgs("uuid");
		$name_en = $this->spArgs("name_en");
		$name_cn = $this->spArgs("name_cn");
		$menu_db_obj = $this->getMenuDbObj();
		//post form
		if ( $name_en ){
			$exist_data = $menu_db_obj->get($uuid);
			$is_new = FALSE;
			if ( !$exist_data ){
				$exist_data = array();
				$is_new = TRUE;
			}
			$sort = intval($this->spArgs("sort"));
			$active = $this->spArgs("active");
			if (   $is_new
					OR $exist_data['name_en'] != $name_en
					OR  $exist_data['name_cn'] != $name_cn
					OR $exist_data['order'] != $sort
					OR $exist_data['active'] != $active
					){
	
				$exist_data['name_en'] = $name_en;
				$exist_data['name_cn'] = $name_cn;
				$exist_data['order'] = $sort;
				$exist_data['active'] = $active;
	
				if ( $is_new ) {
					$menu_db_obj->create($exist_data);
				} else {
					$menu_db_obj->update(array($menu_db_obj->pk=>$exist_data[$menu_db_obj->pk]),$exist_data);
				}
			}
			$this->jump(spUrl($this->controller, 'index'));
		}
	
		
		$this->menu_data = $menu_db_obj->get($uuid);
	}
	function delete(){
		$uuid = $this->spArgs("uuid");
		$menu_db_obj = $this->getMenuDbObj();
		$menu_db_obj->model_version_delete_byPk($uuid,true,true);
		$this->jump(spUrl($this->controller, 'index'));
	}
	private function getMenuDbObj() {
		$menu_db_obj = spClass("model_".$this->type);
		return $menu_db_obj;
	}
	public function showmenuitem() {
		$uuid = $this->spArgs("menu_uuid");
		SessionUtil::setVar('ui_view_current_menu_uuid',$uuid);
		$this->jump(spUrl($this->controller.'_item','index'));
	}
	function setshowsetting(){
		$value = $this->spArgs("v");
		SessionUtil::setVar('ui_view_show_setting',$value);
		$this->jump(spUrl($this->controller,'index'));
	}
}

?>